package com.yicheng.statistics.common

import java.text.SimpleDateFormat
import java.util.Date

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.DateTimeZone

/**
 * 
 */
object TimeUtils {
  
  /**
   * start 开始时间 24小时记
   * interval 时间长度 小时计算
   */
  def getStartEndTimeByHour(start:Int,interval:Int):(DateTime,DateTime) = {
    val now = DateTime.now()
    val theDayBefor = now.minusDays(1)
    val day = theDayBefor.toString(DateTimeFormat.forPattern("yyyy-MM-dd"))
    val starttime = DateTime.parse(day).plusHours(start)
    val endtime = starttime.plusHours(interval)
    (starttime,endtime)
  }
  
  /// <summary>每日</summary>
  val ANALYSE_PERIOD_DAY = 1

  /// <summary>每周</summary>
  val ANALYSE_PERIOD_WEEK = 2

  /// <summary>每月</summary>
  val ANALYSE_PERIOD_MONTH = 3
  def checkDateInRange(gpsDate: DateTime, analysePeriod: Int, periodValue: String, startTime: String, endTime: String): Boolean = {
    val formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss")
    val inRange = false
    if (analysePeriod == ANALYSE_PERIOD_WEEK) {
      //val dayOfWeek = gpsDate.dayOfWeek().get().toString
      val dayOfWeek = gpsDate.dayOfWeek().getAsShortText
      if (!periodValue.contains(dayOfWeek)) {
        return inRange
      }
    }
    if (analysePeriod == ANALYSE_PERIOD_MONTH) {
      val dayOfMonth = gpsDate.dayOfMonth().get().toString
      if (!periodValue.contains(dayOfMonth)) {
        return inRange
      }
    }
    val years = gpsDate.toString("YYYY-MM-dd")
    val start = years + " " + startTime
    val end = years + " " + endTime

    var firstDate = DateTime.parse(start, formatter)
    val secondDate = DateTime.parse(end, formatter)

    if (firstDate.isAfter(secondDate)) {
      firstDate = firstDate.plusDays(-1)
    }
    gpsDate.getMillis > firstDate.getMillis && gpsDate.getMillis <= secondDate.getMillis
  }
  
  /**
   * 判断是否在夜间解析时间段
   * start 00:00:00 时间段开始
   * end 00:00:00 时间段结束
   * day 计算时间段的属于那一天
   * time 
   */
  def isNight(start:String, end:String , time:Date): Boolean = {
    var isNight = false
    val starttime = time2DateTime(start, new DateTime(time)).getMillis
    val endtime = time2DateTime(end, new DateTime(time)).getMillis
    isNight = (starttime <= time.getTime()) && (time.getTime() < endtime)
    isNight
  }
  
  /**
   * 超过时间阈值
   * start 开始时间
   * end 结束时间
   * timesec 时间长度 秒
   */
  def inTimeRang(start:Date, end:Date, timesec:Int): Boolean = {
    (end.getTime - start.getTime) > timesec
  }
  
  /**
   * 计算时间间隔 返回 秒
   */
  def secondsBetweenTime(start:Date, end:Date): Long = {
    val seconds = end.getTime - start.getTime
    seconds
  }
  
  def inTimeRang(start:String,end:String, time:Date): Boolean = {
    val dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd")
    val dayStr = dateTimeFormat.format(time)
    val starttime = DateTime.parse(dayStr+ " " + start, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
    val endtime = DateTime.parse(dayStr +" " + end, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
    (starttime.getMillis <= time.getTime) && (endtime.getMillis > time.getTime)
  }
  
  /**
   * time 00:00:00 新时间时分秒
   * day DateTime 当前时间
   */
  def time2DateTime(time:String,day:DateTime): DateTime = {
    val dayStr = day.toString("yyyy-MM-dd")
    val newDay = dayStr + " " + time
    val dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
    DateTime.parse(newDay, dateTimeFormat)
  }
  
  def getPreDateTime():DateTime = {
    DateTime.now().minusDays(-1)
  }
  
  def main(args: Array[String]): Unit = {
    println(getStartEndTimeByHour(0, 1))
    
    println(time2DateTime("01:22:33", DateTime.now()).toString("yyyy-MM-dd HH:mm:ss"))
    println(inTimeRang("10:11:00", "14:12:23", DateTime.now().toDate()))
  }
  
}